/**
 * 
 */
package info.niwota.frames;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.os.Parcelable;

/**
 * @author qiangli
 *
 */
public class ImageFrame {

	public static final String DEFAULT_FRAMENAME = "Pictures";

	public static final int BATCH_SIZE = 10;
	
	private boolean editable;
	
	private final ImageList list = new ImageList();
	
	private String name;  //peer's label name starts with ~

	private boolean peer;

	private int position;

	private File store; //local 
	
	private boolean dirty;

	private List<Uri> references;

	private long lastModified;

	public ImageFrame() {
	}

	public void add(ContentResolver cr, int loc, Uri uri) {
		UriImage ui = new UriImage(cr, uri);
		list.getImageList().add(loc, ui);
		
		dirty = true;
	}

	public void clear() {
		list.clear();
	}

	public boolean delete() {
		boolean b = store.delete();
		store = null;
		list.clear();
		return b;
	}

	public int getCount() {
		return (list == null ? 0 : list.getCount());
	}

	public IImage getCurrentImage() {
		return list.getImageAt(position);
	}

	public String getName() {
		return name;
	}
	
	public String getDisplayName() {
		if (name == null) {
			return "";
		}
		return (name.charAt(0) == '~' ? name.substring(1) : name);
	}

	public int getPosition() {
		return position;
	}

	public File getStore() {
		return store;
	}

	public boolean isEditable() {
		return  editable;
	}

	public boolean isEmpty() {
		return (list == null || list.getCount() == 0);
	}

	public boolean isPeer() {
		return peer;
	}
	
	public void restore(Context ctx) throws Exception {
		dirty = false;
		
		if (store == null) {
			return;
		}
		
		position = restoreFrame(ctx);
		//
		int cnt = list.getCount();
		if (cnt == 0) {
			position = 0;
			return;
		} else if (cnt <= position) {
			position = cnt - 1;
		}
	}

	private int restoreFrame(Context ctx) throws Exception {
		if (store == null) {
			return 0;
		}
		List<Uri> uris = new ArrayList<Uri>();
		
		int pos = FrameHelper.load(ctx, uris, this);
		
		int size = uris.size();
		if (size == 0) {
			return 0;
		}
		
		ContentResolver cr = ctx.getContentResolver();
		int idx = 0;
		List<IImage> l = list.getImageList();
		for (Iterator<Uri> i = uris.iterator(); i.hasNext();) {
			Uri uri = i.next();
			
			UriImage ui = (peer ? new PeerUriImage(this, ctx, uri, idx) : new UriImage(cr, uri));
			l.add(idx, ui);
			
			idx++;
		}
		return pos;
	}
	
	public void save(Context ctx) {
		saveFrame(ctx);
	}

	private void saveFrame(Context ctx) {
		
		if (!dirty) {
			return;
		}
		
		if (store == null) {
			return;
		}
		
		List<IImage> l = list.getImageList();
		int size = l.size();

		ArrayList<Uri> uris = new ArrayList<Uri>(size);
		for (int i = 0; i < size; i++) {
			Uri u = l.get(i).fullSizeImageUri();
			uris.add(u);
		}
		
		FrameHelper.save(uris, this);
		
		dirty = false;
	}

	public void setPosition(int pos) {
		position = pos;
		
		dirty = true;
	}
	
	public void setStore(File file) {
		store = file;
		dirty = false;
		
		String n =  file.getName();
		name = n;
		peer = (n.startsWith("~"));
		editable = (!peer);
		
		list.clear();
	}

	/**
	 * format: ~peer label
	 * 
	 * @return
	 */
	public String[] getPeerLabel() {
		String[] sa = name.substring(1).split(" ", 2);
		return sa;
	}

	public void removeCurrent() {
		if (list == null) {
			return;
		}
		list.removeImageAt(position);
		
		dirty = true;
	}

	@Override
	public String toString() {
		return "ImageFrame [editable=" + editable + ", list=" + list
				+ ", name=" + name + ", peer=" + peer + ", position="
				+ position + ", store=" + store + ", dirty=" + dirty + "]";
	}

	public ArrayList<? extends Parcelable> getParcelableList() {
		ArrayList<Parcelable> plist = new ArrayList<Parcelable>();
		for (IImage image : list.getImageList()) {
			plist.add(image.fullSizeImageUri());
		}
		return plist;
	}

	public IImage getImage(int pos) {
		if (list == null) {
			return null;
		}
		return list.getImageAt(pos);
	}

	public ImageList getList() {
		return list;
	}

	public void setReferences(List<Uri>  references) {
		this.references = references;
	}

	public List<Uri> getReferences() {
		return references;
	}
	
	public static int getBatchNo(int pos) {
		return pos / BATCH_SIZE;
	}

	public long getLastModified() {
		return lastModified;
	}

	public void setLastModified(long lastModified) {
		this.lastModified = lastModified;
	}
}
